package org.bsc.mgwt.client.dialog;

import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.Dialog;
import com.googlecode.mgwt.ui.client.dialog.DialogPanel;
import com.googlecode.mgwt.ui.client.dialog.HasTitleText;
import com.googlecode.mgwt.ui.client.dialog.PopinDialog;
import com.googlecode.mgwt.ui.client.theme.base.DialogCss;
import com.googlecode.mgwt.ui.client.theme.base.InputCss;

/**
 * Deprecated use BaseInputDialog instead
 * 
 * @author bsorrentino
 *
 */
@Deprecated
public abstract class AbstractInputDialog implements HasTitleText, Dialog {

		/**
		 * The callback used when buttons are taped
		 * 
		 * 
		 */
		public interface InputCallback<T extends AbstractInputDialog> {
			
			/**
			 * Called if ok button is taped
			 * 
			 * @return true if dialog must be closed. Otherwise false
			 */
			public boolean onOK( T source );

			/**
			 * called if cancel button is taped
			 */
			public void onCancel( T source );
		}
		
		
		private PopinDialog popinDialog;
		private DialogPanel outerPanel;
		private final InputCallback callback;

		/**
		 * Construct an alert dialog
		 * 
		 * @param title - the title of the dialog
		 * @param text - the text of the dialog
		 */
		public AbstractInputDialog(String title, InputCallback<? extends AbstractInputDialog> callback ) {
			this(	MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(), 
					MGWTStyle.getTheme().getMGWTClientBundle().getInputCss(),
					title, 
					callback);
			
		}

		/**
		 * Construct an alert dialog
		 * 
		 * @param css - the css to use
		 * @param title - the title of the dialog
		 * @param text - the text of the dialog
		 */
		public AbstractInputDialog(DialogCss css, InputCss inputCss, String title, InputCallback<? extends AbstractInputDialog> _callback) {
			this.callback = _callback;
			
			inputCss.ensureInjected();
			css.ensureInjected();
				
			popinDialog = new PopinDialog(css);
			outerPanel = new DialogPanel();
			outerPanel.showCancelButton(true);
			outerPanel.showOkButton(true);

			Widget main = initContentPanel(inputCss);
			
			outerPanel.getContent().add(main);
			popinDialog.add(outerPanel);

			outerPanel.getOkButton().addTapHandler(new TapHandler() {

				@Override
				public void onTap(TapEvent event) {
					boolean close = true;;
					if( callback != null ) {
						close = callback.onOK( AbstractInputDialog.this );
					}
					if( close )
						popinDialog.hide();
					
				}

			});
			
			outerPanel.getCancelButton().addTapHandler(new TapHandler() {

				@Override
				public void onTap(TapEvent event) {
					popinDialog.hide();
					if( callback != null ) callback.onCancel( AbstractInputDialog.this );
				}

			});

			setTitleText(title);

			
		}

		protected abstract Widget initContentPanel( InputCss css );
		
		@Override
		public void setTitleText(String text) {
			outerPanel.getDialogTitle().setText(text);

		}

		@Override
		public String getTitleText() {
			return outerPanel.getDialogTitle().getText();
		}

		public void show() {
			popinDialog.center();
		}

		public void hide() {
			popinDialog.hide();
		}

}